package com.zss.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zss.admin.service.AdminEmployeeService;
import com.zss.pojo.Employee;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("admin/employee")
public class AdminEmployeeController {

    @Resource
    AdminEmployeeService employeeService;

    /**
     * 进入列表页面
     * @return
     */
    @RequestMapping("list")
    public String list(){
        return "admin/employee/list";
    }

    /**
     * 获取列表数据
     * @return
     */
    @RequestMapping("getList")
    @ResponseBody
    public List<Employee> getList(){
        LambdaQueryWrapper<Employee> queryWarpper = new LambdaQueryWrapper<Employee>();
        queryWarpper.orderByAsc(Employee::getStatus);
        return employeeService.list(queryWarpper);
    }

    /**
     * 区域添加或修改
     * @param employee
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public void add(@RequestBody Employee employee){
        employee.setStatus("0");
        employeeService.saveOrUpdate(employee);
    }

    /**
     * 区域回显
     * @param id
     * @return
     */
    @RequestMapping("findById/{id}")
    @ResponseBody
    public Employee findById(@PathVariable int id){
        return employeeService.getById(id);
    }

    /**
     * 根据id删除
     * @param id
     */
    @RequestMapping("deleteById/{id}")
    @ResponseBody
    public void deleteById(@PathVariable int id){
        employeeService.removeById(id);
    }

    /**
     * 根据id跟新状态
     * @param id
     */
    @RequestMapping("updateStatus/{id}/{status}")
    @ResponseBody
    public void deleteById(@PathVariable int id,@PathVariable String status){
        LambdaQueryWrapper<Employee> queryWarpper = new LambdaQueryWrapper<Employee>();
        queryWarpper.eq(Employee::getId,id);
        Employee employee = new Employee();
        employee.setStatus(status);
        employeeService.update(employee,queryWarpper);
    }

    @RequestMapping("findByAid/{aid}")
    @ResponseBody
    public List<Employee> findByAid(@PathVariable Integer aid){
        LambdaQueryWrapper<Employee> queryWarpper = new LambdaQueryWrapper<Employee>();
        queryWarpper.eq(Employee::getAid,aid);
        List<Employee> list = employeeService.list(queryWarpper);
        return list;
    }

}
